import requests
from PIL import Image
import io
import ddddocr

slide = ddddocr.DdddOcr(det=False, ocr=False,show_ad=False)

session = requests.Session()

res = session.get('https://bm.ruankao.org.cn/sign/captchaImg?0.08734725060895676')
with open(f'./static/yzm/rk.png', 'wb') as f:
    f.write(res.content)

img = Image.open(f'./static/yzm/rk.png')
img_not_full = img.crop((0,0,240,150))
img_not_full_bytes = io.BytesIO()
img_not_full.save(img_not_full_bytes, format='PNG')
# img_not_full.show()

img_target = img.crop((0,151,50,300))
img_target_bytes = io.BytesIO()
img_target.save(img_target_bytes, format='PNG')
# img_target.show()

# img_full = img.crop((0,301,240,450))
# img_full_bytes = io.BytesIO()
# img_full.save(img_full_bytes, format='PNG')
# img_full.show()

res1 = slide.slide_match(img_target_bytes.getvalue(), img_not_full_bytes.getvalue())
# res2 = slide.slide_comparison(img_not_full_bytes.getvalue(), img_full_bytes.getvalue())
# print(res1,res2)
offset = res1["target"][0]

captcha_res = session.get(f'https://bm.ruankao.org.cn/sign/checkTnCode?p={offset}')
print(captcha_res.json())

login_res = session.post('https://bm.ruankao.org.cn/sign/inok',data={
's1':'MzY1MzQzNDM0NTM0NQ==',
's2':'68f95fb4cb6ac51ebeb3706d85c0a975',
's3':offset
})

print(login_res.json())